package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.exception.BaseException;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.ChatRobot;
import cn.jdy.plugins.entity.WatchUser;


/**
 * @title 页签操作
 * @author Lutan
 * @since 1-12-2015
 */
@Service
public class WatchUserService extends BaseDao<WatchUser>{

	@Autowired
	private ConfService confService;
	
	/**
	 * 按条件查询
	 * @param Kindergarten
	 * @return
	 */
	public List<WatchUser> query(WatchUser obj, String... date) {
		if(null == obj) return super.findAll();
		if(obj.getiDisplayLength() > 0){
		  return super.find(createFilter(obj, date),obj.getiDisplayStart(),obj.getiDisplayLength());
		}
		return super.find(createFilter(obj, date));
	}

	public Long count(WatchUser obj, String... date) {
		return super.count(createFilter(obj, date));
	}
	
	/**
	 * 拼装条件(直播id、创建时间)
	 * @param Kindergarten
	 * @param date
	 * @return
	 */
	private DetachedCriteria createFilter(WatchUser obj, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  
	
	  
	 if(null != obj.getLiveId()){
			  dc.add(Restrictions.eq("liveId", obj.getLiveId()));
		 }

	 
	
	 if(!StringUtil.isNullOrEmpty(obj.getUserSex())){
		  dc.add(Restrictions.eq("userSex", obj.getUserSex()));
		}
	 
	 if(!StringUtil.isNullOrEmpty(obj.getUserNickName())){
		  dc.add(Restrictions.like("userNickName", obj.getUserNickName(),MatchMode.ANYWHERE));

		}
	  
		if (date.length > 0 && !StringUtil.isNullOrEmpty(date[0])
				&& !StringUtil.isNullOrEmpty(date[1])) {
			dc.add(Restrictions.between("createTime", date[0], date[1]));
		}
	  dc.addOrder(Order.asc("createTime"));
		return dc;
	}
	

	 public List<WatchUser> querypage(WatchUser sp){
	       return super.find(createFilter(sp),sp.getPageNo(),18);
	  }
	public Long count(WatchUser sp){
		return super.count(createFilter(sp));
	}

  public List<WatchUser> findbytype(int type, String addres, int i, int limitMax) {
    DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
    dc.add(Restrictions.eq("status", "1"));
        if(null != addres){
          dc.add(Restrictions.eq("address", addres));
        }
      if(type==0){// 根据类排序，0 表示目前根据创建时间排序 ， 以后可能会根据推广，销售量等等排序
        dc.addOrder(Order.desc("createTime"));
      }else{
        //以后会有字段或者方案
      }
      
      return super.find(dc,i,limitMax);
  }
  /**
	 *  删除
	 *
	 * @return String 删除用户信息的成功、失败信息
	 * @description: 删除指定用户标识的系统用户信息, 对多个系统用户信息的删除操作
	 * @param: id 用户标识（多个用户时用,号隔开）
	 * @author:lutan
	 * @date: 2017-03-23 16:40
	 */
	@Transactional
	public String delUserById(String userIds) {
		String backInfo = "0";
		try {
			String[] ids = userIds.split(",");
			// 删除用户
			super.delete(ids, "id");
			backInfo = "1";
		} catch(Exception e) {
			backInfo = "0"; // 删除失败
			BaseLog.e(this.getClass(), "删除页签", e);
			throw new BaseException("删除页签出错！", e);
		}
		return backInfo;
	}
}
